<?php
namespace app\shop\controller;
use app\admin\model\User;
use app\AdminRest;
use app\ApiRest;
use app\shop\model\IndexUserInfo;
use app\shop\model\PurchaseCate;
use app\shop\model\PurchaseCompany;
use app\shop\model\PurchaseUserTmp;
use think\App;
use app\shop\model\PurchaseTmp as Model;

use think\facade\Db;

class IndexPurchase extends ApiRest
{

    protected $model;

    protected $cate_model;

    protected $user_cate_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->cate_model = new PurchaseCate();

        $this->user_cate_model = new PurchaseUserTmp();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:34
     * @功能说明:自提点列表
     */
    public function tmpList(){

        $input = $this->_param;

        $user_info_model = new IndexUserInfo();

        $company_id = $user_info_model->getCompanyId(['fans_id'=>$input['staff_id']],$this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //未删除
        $dis[] = ['a.status','=',1];

        $where[] = ['b.company_id','=',$company_id];

        $where[] = ['a.public_company','=',1];

        $data = $this->model->indexTmpList($dis,$where,10);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-11 16:39
     * @功能说明:模版详情
     */
    public function tmpInfo(){

        $input = $this->_param;

        $dis = [
            'id' => $input['id']
        ];

        $data = $this->model->tmpInfo($dis);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-10 17:50
     * @功能说明:个人中心的模版
     */
    public function userTmpList(){

        $input = $this->_param;

        $user_info_model = new IndexUserInfo();

        $company_id = $user_info_model->getCompanyId(['fans_id'=>$input['staff_id']],$this->_uniacid);

        $dis[]   = ['a.uniacid','=',$this->_uniacid];
        //未删除
        $dis[]   = ['a.status','=',1];

        $dis[]   = ['c.user_id','=',$this->getUserId()];

        $dis[]   = ['c.staff_id','=',$input['staff_id']];

        $where[] = ['b.company_id','=',$company_id];

        $where[] = ['a.public_company','=',1];

        $data = $this->model->userIndexTmpList($dis,$where,10);

        return $this->success($data);

    }





    /**
     * @author chenniang
     * @DataTime: 2020-12-09 16:57
     * @功能说明:
     */
    public function tmpCateList(){

        $input = $this->_param;

        $user_model = new \app\shop\model\IndexUser();

        $user_model->where(['id'=>$this->getUserId()])->update(['last_staff_id'=>$input['staff_id']]);

        $this->getUserInfo()['last_staff_id'] = $input['staff_id'];

        setCache( $this->autograph,$this->getUserInfo(),7200, $this->_uniacid );

        $is_public = $this->model->where(['id'=>$input['id']])->value('public_cate');

        $dis = [

            'a.tmp_id'   => $input['id'],

            'a.uniacid'  => $this->_uniacid,

            'b.status'   => 1,

            'a.staff_id' => $input['staff_id'],

            'a.user_id'  => $this->getUserId()
        ];
        //用户自己diy的
        $tmpCate = $this->user_cate_model->selectList($dis);

        if(empty($tmpCate)){

            unset($dis['a.staff_id']);

            unset($dis['a.user_id']);
            //默认的
            $tmpCate = $this->cate_model->selectList($dis,$is_public);
        }

        return $this->success($tmpCate);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-10 09:25
     * @功能说明:分类列表
     */
    public function allCateList(){

        $input = $this->_param;

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1,

            'pid'     => 0

        ];

        $map = [];

        if(!empty($input['title'])){

            $map[] = ['title','like','%'.$input['title'].'%'];

        }

        $cate = Db::name('longbing_card_shop_type')->where($dis)->where($map)->order('top')->field('id,title')->select()->toArray();

        $is_public = $this->model->where(['id'=>$input['id']])->value('public_cate');

        $dis = [

            'a.tmp_id' => $input['id'],

            'a.uniacid'=> $this->_uniacid,

            'b.status' => 1,

            'a.staff_id' => $input['staff_id'],

            'a.user_id'  => $this->getUserId()

        ];
        //用户自己diy的
        $tmpCate = $this->user_cate_model->selectList($dis);

        if(empty($tmpCate)){

            unset($dis['a.staff_id']);

            unset($dis['a.user_id']);
            //默认的
            $tmpCate = $this->cate_model->selectList($dis,$is_public  );
        }

        $tmpCate = !empty($tmpCate)?array_column($tmpCate,'id'):[];

        if(!empty($cate)){

            foreach ($cate as $key => $value){

                if(in_array($value['id'],$tmpCate)){

                    $cate[$key]['is_select'] = 1;

                }else{

                    $cate[$key]['is_select'] = 0;

                }
            }
        }

        return $this->success($cate);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-10 09:35
     * @功能说明:增加用户自己的模版
     */
    public function addUserTmp(){

        $input = $this->_input;

        $this->user_cate_model->where(['staff_id'=>$input['staff_id'],'user_id'=>$this->getUserId(),'tmp_id'=>$input['tmp_id']])->delete();

        foreach ($input['cate'] as $key => $value){

            $insert[$key] = [

                'user_id' => $this->getUserId(),

                'uniacid' => $this->_uniacid,

                'tmp_id'  => $input['tmp_id'],

                'cate_id' => $value,

                'staff_id'=> $input['staff_id']

            ];

        }

        $res = $this->user_cate_model->saveAll($insert);

        return $this->success($res);
    }






}
